package com.vasthu.core.service;

import com.vasthu.api.dao.ItemDAO;
import com.vasthu.api.domain.Item;
import com.vasthu.api.service.ItemManagerService;
import com.vasthu.core.dao.ItemDAOImpl;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.utility.VstDBManager;
import java.util.*;
import java.sql.*;

public class ItemManagerServiceImpl implements ItemManagerService {

	private ItemDAO itemDao;

	public ItemManagerServiceImpl() {
		itemDao = new ItemDAOImpl();
	}
	
	public boolean insertItem(Item item) {
		if (item == null)
			return false;
			
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = itemDao.insertItem(conn, item);
		} catch (Exception ex) {
			System.out.println("ERROR : ItemManagerServiceImpl->insertItem()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public Item getItemById(long itemId) {
		Connection conn = VstDBManager.get().getConnection();
		Item item = null;
		try {
			item = itemDao.getItemById(conn, itemId);
		} catch (Exception ex) {
			System.out.println("ERROR : ItemManagerServiceImpl->deleteItemById()");
		}
		
		VstDBManager.get().close();
		return item;
	}
	
	public Item getItemByCode(String itemCode) {
		Connection conn = VstDBManager.get().getConnection();
		Item item = null;
		try {
			item = itemDao.getItemByCode(conn, itemCode);
		} catch (Exception ex) {
			System.out.println("ERROR : ItemManagerServiceImpl->deleteItemByName()");
		}
		
		VstDBManager.get().close();
		return item;
	}
	
	public boolean deleteItemById(long itemId) {
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = itemDao.deleteItemById(conn, itemId);
		} catch (Exception ex) {
			System.out.println("ERROR : ItemManagerServiceImpl->deleteItemById()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public List<String> getAllItemCodes() {
		List<Item> itemList = new ArrayList<Item>();
		List<String> itemNameList = new ArrayList<String>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			itemList = itemDao.getAllItems(conn);
			for (Item item : itemList) {
				itemNameList.add(item.getItemCode());
			}
		} catch (Exception ex) {
			System.out.println("ERROR : ItemManagerServiceImpl->getAllItemNames()");
		}
		
		VstDBManager.get().close();
		
		return itemNameList;
	}

	public List<VstTableItemSelectable> getAllItemsForTable() {
		List<Item> itemList = new ArrayList<Item>();
		List<VstTableItemSelectable> dataList = new ArrayList<VstTableItemSelectable>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			itemList = itemDao.getAllItems(conn);
			for (Item item : itemList) {
				dataList.add(item);
			}
		} catch (Exception ex) {
			System.out.println("ERROR : ItemManagerServiceImpl->getAllItemsForTable()");
		}
		
		VstDBManager.get().close();
		
		return dataList;
	}

}